package com.golddaymicromall.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.bean.ProductInfo;
import com.golddaymicromall.model.HttpModel;

import java.util.List;

/**
 * Created by dawd on 2016/3/18.
 */
public class ProductAdapter extends BaseAdapter {
    private List<ProductInfo> list;
    private Context ctx;


    public ProductAdapter(List<ProductInfo> list, Context ctx) {
        this.list = list;
        this.ctx = ctx;
        if(ctx == null){
            return;
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder hold;
        if (convertView == null) {
            hold = new ViewHolder();
            convertView = View.inflate(ctx, R.layout.item_productlist, null);
            hold.mImg = (ImageView) convertView.findViewById(R.id.GoodsList_Img);
            hold.mDis = (TextView) convertView.findViewById(R.id.GoodsList_DisTv);
            hold.mPrice = (TextView) convertView.findViewById(R.id.GoodsList_PriceTv);
            convertView.setTag(hold);
        } else {
            hold = (ViewHolder) convertView.getTag();
        }
        Glide.with(ctx)
                .load(new HttpModel().IMGURL + list.get(position).getImage())
                .placeholder(R.drawable.loading_4)
                .centerCrop()
                .into(hold.mImg);
        hold.mPrice.setText("￥" + list.get(position).getPrice());
        hold.mDis.setText(list.get(position).getProductname());
        return convertView;
    }

    private class ViewHolder {
        TextView mPrice;
        TextView mDis;
        ImageView mImg;
    }
}
